package com.gm.wholesale.module.report.ui.tab;

import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnRangeDatePickListener;
import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.util.timeUtils.DateTimeUtils;
import com.gm.commonlib.util.viewUtils.KeyboardUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.timePickerView.TimePickerViewUtils;
import com.gm.wholesale.R;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.report.bean.CustormReportBean;
import com.gm.wholesale.module.report.model.ReportModel;
import com.gm.wholesale.module.report.view.ReportTabTimeView;
import com.gm.wholesale.module.report.view.ShowCustomReportView;

import java.util.Date;
import java.util.List;

public class CustormReportFragment extends BaseFragment implements View.OnClickListener {
    private EditText custormNameEd;
    private ReportTabTimeView todayTv;
    private ReportTabTimeView weekTv;
    private ReportTabTimeView monthTv;
    private TextView choiceTimeTv;
    private ListView listView;
    private String mTimeBegin;
    private String mTimeEnd;

    private String mChoiceTime;

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_custorm_report, parent);
        initView(view);
    }

    private void initView(View view) {
        custormNameEd = view.findViewById(R.id.custormNameEd);
        todayTv = view.findViewById(R.id.todayTv);
        weekTv = view.findViewById(R.id.weekTv);
        monthTv = view.findViewById(R.id.monthTv);
        choiceTimeTv = view.findViewById(R.id.choiceTimeTv);
        listView = view.findViewById(R.id.listView);
        todayTv.showCheck();
        mTimeBegin = DateTimeUtils.formatDateTime(System.currentTimeMillis(), DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
        mChoiceTime=mTimeBegin+" 至 "+mTimeBegin;
        mTimeEnd=mTimeBegin;
        setChoiceTime();
        getData();
        todayTv.setOnClickListener(this);
        weekTv.setOnClickListener(this);
        monthTv.setOnClickListener(this);
        choiceTimeTv.setOnClickListener(this);
        custormNameEd.setOnEditorActionListener((v, actionId, event) -> {

            if (actionId== EditorInfo.IME_ACTION_SEARCH) {
                getData();
                KeyboardUtils.hideKeyboard(v);
            }
            return false;

        });
    }
    private void setChoiceTime(){
        choiceTimeTv.setText(mChoiceTime);
    }

    private void getData(){
        ArrayMap<String, Object> queryData = new ArrayMap<>();
        queryData.put("dateBegin", mTimeBegin+" 00:00:00");
        queryData.put("dateEnd", mTimeEnd+" 23:59:59");
        queryData.put("customerName",custormNameEd.getText().toString());

        ReportModel.getCustomerReport(null, queryData, new BaseHttpObserver<BasePageData<CustormReportBean>>() {
            @Override
            protected void handleResultOkResp(BasePageData<CustormReportBean> resp) {
                    showView(resp.getData().getDatas());
            }
        });
    }
    private void showView(List<CustormReportBean> reportGoodBeans){
          listView.setAdapter(new CommonAdapter<CustormReportBean>(getContext(),reportGoodBeans,R.layout.list_custorm_report_item) {
              @Override
              public void convert(ViewHolder viewHolder, CustormReportBean data) {
              viewHolder.setText(R.id.custormNameTv,data.getCustomerName());
              viewHolder.setText(R.id.custormPriceTv,data.getSalesAmount()+"元");
              viewHolder.setText(R.id.revicePriceTv,data.getPaidAmount()+"元");
              viewHolder.getView(R.id.showDetailTv).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      showCustormByGoods(data);
                  }
              });
              }
          });
    }

    private void showCustormByGoods(CustormReportBean custormReportBean){
        CustomBean customBean=new CustomBean();
        customBean.setName(custormReportBean.getCustomerName());
        customBean.setId(custormReportBean.getCustomerID());
        ShowCustomReportView showCustomReportView = new ShowCustomReportView(getActivity());
        showCustomReportView.showCustom(customBean, (IBaseView) getActivity());
        Popupwindow.showPopCenterView(getActivity(), showCustomReportView);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.todayTv) {
            mTimeBegin = DateTimeUtils.formatDateTime(System.currentTimeMillis(), DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
            mChoiceTime=mTimeBegin+" 至 "+mTimeBegin;
            setChoiceTime();
            todayTv.showCheck();
            weekTv.showUnCheck();
            monthTv.showUnCheck();
            getData();
        } else if (view.getId() == R.id.weekTv) {
            mTimeBegin = DateTimeUtils.getWeekStart();
            mTimeEnd = DateTimeUtils.getWeekEnd();
            mChoiceTime=mTimeBegin+" 至 "+mTimeEnd;
            todayTv.showUnCheck();
            weekTv.showCheck();
            monthTv.showUnCheck();
            setChoiceTime();
            getData();
        } else if (view.getId() == R.id.monthTv) {
            mTimeBegin = DateTimeUtils.getMonthStart();
            mTimeEnd = DateTimeUtils.getMonthEnd();
            mChoiceTime=mTimeBegin+" 至 "+mTimeEnd;
            todayTv.showUnCheck();
            weekTv.showUnCheck();
            monthTv.showCheck();
            setChoiceTime();
            getData();
        }
        else if (view.getId()==R.id.choiceTimeTv){
            long startTime=DateTimeUtils.getTimeStringToLong(mTimeBegin+" 00:00:01",DateTimeUtils.LONG_DATETIME_FORMAT_STRING);
            long endTime=DateTimeUtils.getTimeStringToLong(mTimeEnd+" 23:59:59",DateTimeUtils.LONG_DATETIME_FORMAT_STRING);
            CalendarPicker calendarPicker= TimePickerViewUtils.showTimePicker(getActivity(),startTime,endTime);
            calendarPicker.setOnRangeDatePickListener(new OnRangeDatePickListener() {
                @Override
                public void onRangeDatePicked(@NonNull Date startDate, @NonNull Date endDate) {
                    setViewUnCheck(todayTv);
                    setViewUnCheck(weekTv);
                    setViewUnCheck(monthTv);
                    mTimeBegin=DateTimeUtils.formatDateTime(startDate,DateTimeUtils.YMD);
                    mTimeEnd=DateTimeUtils.formatDateTime(endDate,DateTimeUtils.YMD);
                    mChoiceTime=mTimeBegin +" 至 "+mTimeEnd;
                    setChoiceTime();
                    getData();
                }
            });
        }

    }

    private void setViewUnCheck(ReportTabTimeView view){
        view.showUnCheck();
    }
}
